<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>人员列表</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/base.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 0 30px;
        }

        table {
            /* height: calc(100vh - 143px); */
            font-weight: 500;
            height: 300px;
        }

        .table td,
        .table th {
            border: none;
        }

        .table thead th {
            background-color: #f7f7f7;
            border-bottom: none;
            font-weight: 600;
            font-size: 16px;
            color: #999999;
            height: 80px;
            line-height: 80px;
            padding: 0;
            position: sticky;
            top: 0px;
        }

        .table tbody+tbody {
            border-top: none;
        }

        .user-table {
            width: 100%;
            /* 表格宽度为容器宽度的100% */
            height: 100vh;
            overflow-y: auto;
        }

        .user-table .header h2 {
            background: #fff;
            /* width: 100%; */
            font-weight: 600;
            font-size: 18px;
            color: #000000;
            height: 70px;
            display: flex;
            align-items: center;
            margin-bottom: 0;
        }

        .pagination input {
            width: 80px;
            text-align: center;
        }


        .user-table .header {
            display: flex;
            height: 70px;
        }

        .user-table .search-box {
            padding-left: 20px;
            height: 70px;
            display: flex;
            align-items: center;
            margin-bottom: 0;
        }

        .search-box input {
            width: 300px;
            padding: 5px 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .user-table .search-button {
            padding-left: 20px;
            height: 70px;
            display: flex;
            align-items: center;
            margin-bottom: 0;
        }

        .my-button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            font-size: 14px;
            width: 80px;
            height: 40px;
        }

        /* 去除按钮点击时的黑色边框样式 */
        .my-button:focus {
            outline: none;
        }

        .my-button:active {
            background-color: #0056b3;
            /* 点击时改变的背景颜色 */
        }

        .table-box {
            height: calc(100vh - 140px);
            overflow-y: auto;
        }

        .user-table th,
        .user-table td {
            text-align: center;
            vertical-align: middle;
            padding: 10px;
            height: 50px;
            /* 统一设置表头和单元格的高度 */
        }

        /* 第一列和第三列的样式 */
        .user-table th:first-child,
        .user-table td:first-child,
        .user-table th:nth-child(3),
        .user-table td:nth-child(3),
        .user-table th:nth-child(5),
        .user-table td:nth-child(5) {
            width: 15%;
            /* 设置第一列和第三列的宽度为15% */
        }

        /* 第四列的样式 */
        .user-table th:nth-child(4),
        .user-table td:nth-child(4) {
            width: 20%;
            /* 设置第四列的宽度为30% */
        }

        /* 其他列的样式（例如第二列和第五列） */
        .user-table th:nth-child(2),
        .user-table td:nth-child(2) {
            width: 20%;
            /* 保持第二列和第五列的宽度为20% */
        }

        .user-table th {
            /* 如果有特定的样式只适用于表头，可以在这里添加 */
        }

        .user-table td {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            max-width: 300px;
            text-align: center;
        }

        .table-box .table .watch-button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            font-size: 14px;
            width: 120px;
            height: 40px;
            text-align: center;
            /* justify-content: center; */
        }

        .table-box .table .delete-button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #ea2b2b;
            color: white;
            cursor: pointer;
            font-size: 14px;
            width: 70px;
            height: 40px;
            text-align: center;
            /* justify-content: center; */
        }

        .table-box .table .modify-button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #7e46e6;
            color: white;
            cursor: pointer;
            font-size: 14px;
            width: 100px;
            height: 40px;
            text-align: center;
            /* justify-content: center; */
        }

        /* 去除按钮点击时的黑色边框样式 */
        .table-box .table button:focus {
            outline: none;
        }

        .table-box .table button:active {
            background-color: #0056b3;
            /* 点击时改变的背景颜色 */
        }
    </style>
</head>

<body>
    <div class=" user-table">
        <div class="header">
            <h2>用户列表</h2>
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="搜索相关用户">
            </div>
            <div class="search-button">
                <button class="my-button" onclick="searchButtonClickHandler()">搜索</button>
            </div>
        </div>
        <div class="table-box">
            <table class="table">
                <thead>
                    <tr>
                        <th>人员id</th>
                        <th>姓名</th>
                        <th>身份</th>
                        <th>操作</th>
                        <th>修改</th>
                        <th>删除</th>
                    </tr>
                </thead>
                <tbody>
                <tbody id="userList">
                    <!-- 奖品列表将动态插入这里 -->
                    <tr>
                        <td>67</td>
                        <td>1</td>
                        <td>NORMAL</td>
                        <td><button class="watch-button" onclick="handleButtonClick('67', '1')">查看中奖记录</button></td>
                        <td><button class="modify-button" onclick="modifyButtonClick('67')">修改信息</button></td>
                        <td><button class="delete-button" onclick="deleteButtonClick('67')">删除</button></td>
                    </tr>

                    <tr>
                        <td>63</td>
                        <td>王丛发</td>
                        <td>NORMAL</td>
                        <td><button class="watch-button" onclick="handleButtonClick('63', '王丛发')">查看中奖记录</button></td>
                        <td><button class="modify-button" onclick="modifyButtonClick('67')">修改信息</button></td>
                        <td><button class="delete-button" onclick="deleteButtonClick('63')">删除</button></td>
                    </tr>

                    <tr>
                        <td>62</td>
                        <td>管理员</td>
                        <td>ADMIN</td>
                        <td><button class="watch-button" onclick="handleButtonClick('62', '管理员')">查看中奖记录</button></td>
                        <td><button class="modify-button" onclick="modifyButtonClick('67')">修改信息</button></td>
                        <td><button class="delete-button" onclick="deleteButtonClick('62')">删除</button></td>
                    </tr>

                    <tr>
                        <td>55</td>
                        <td>张豪</td>
                        <td>NORMAL</td>
                        <td><button class="watch-button" onclick="handleButtonClick('55', '张豪')">查看中奖记录</button></td>
                        <td><button class="modify-button" onclick="modifyButtonClick('67')">修改信息</button></td>
                        <td><button class="delete-button" onclick="deleteButtonClick('55')">删除</button></td>
                    </tr>

                    <tr>
                        <td>53</td>
                        <td>摇摆</td>
                        <td>NORMAL</td>
                        <td><button class="watch-button" onclick="handleButtonClick('53', '摇摆')">查看中奖记录</button></td>
                        <td><button class="modify-button" onclick="modifyButtonClick('67')">修改信息</button></td>
                        <td><button class="delete-button" onclick="deleteButtonClick('53')">删除</button></td>
                    </tr> 

                </tbody>
            </table>
        </div>
    </div>


    <script src="js/jquery.min.js"></script>
    <script>
        var userToken = localStorage.getItem("user_token");

        // 封装AJAX请求的函数
        function fetchUserData() {
            var name = document.getElementById('searchInput').value;
            // 发送AJAX请求
            $.ajax({
                url: '/base-user/find-list?name=' + name,
                type: 'get',
                headers: {
                    // jwt
                    "user_token": userToken
                },
                success: function (result) {
                    if (result.code != 200) {
                        alert("查询用户列表失败！" + result.msg);
                    } else {
                        var users = result.data;
                        // 清空现有的表格内容
                        var userList = $('#userList');
                        userList.empty();
                        users.forEach(function (user) {
                            userList.append(`
                                <tr>
                                    <td>${user.userId}</td>
                                    <td>${user.userName}</td>
                                    <td>${user.identity}</td>
                                    <td><button class="watch-button" onclick="handleButtonClick('${user.userId}', '${user.userName}')">查看中奖记录</button></td>
                                    <td><button class="modify-button" onclick="modifyButtonClick('${user.userId}')">修改信息</button></td>
                                    <td><button class="delete-button" onclick="deleteButtonClick('${user.userId}')">删除</button></td>
                                </tr>
                            `);
                        });
                    } // else end
                },
                error: function (err) {
                    //jwt令牌解析失败
                    console.log(err);
                    if (err != null && err.status == 401) {
                        alert("用户未登录, 即将跳转到登录页!");
                        // 父页面跳转登录页
                        window.parent.location.href = "/blogin.html";
                    }
                }
            });
        }

        // 初始化加载用户数据
        $(document).ready(function () {
            fetchUserData();
        });

        //条件搜索按钮触发
        function searchButtonClickHandler() {
            // var inputValue = document.getElementById('searchInput').value;
            // alert(inputValue);
            fetchUserData();
            // 这里可以编写按钮点击后的具体逻辑，例如弹出提示框等
            // alert(inputValue);
        }

        function handleButtonClick(userId, userName) {
            // console.log("userName:" + userName);
            window.location.href = "/winning-record.html?userId=" + userId + "&userName=" + userName;
        }

        function deleteButtonClick(userId) {
            var res = confirm("确定要删除id为" + userId + "的用户吗?");
            if (res == false) {
                return;
            }
            $.ajax({
                url: '/user/delete?userId=' + userId,
                type: 'post',
                headers: {
                    //jwt
                    "user_token": userToken
                },
                dataType: 'json',
                success: function (result) {
                    if (result.code != 200) {
                        alert("删除人员失败! " + result.msg);
                    } else {
                        if (result.data == true) {
                            fetchUserData();
                        } else {
                            alert("删除人员失败!");
                        }
                    }
                },
                error: function (result) {
                    alert("删除人员失败!");
                }
            });
        }

        function modifyButtonClick(userId) {
            // confirm("确定要修改人员id为" + userId + "的个人信息吗?")
            window.location.href = "/modify-user.html?userId=" + userId;
        }

    </script>
</body>

</html>